import { createRouter, createWebHistory } from "vue-router";
import LoginView from "./views/LoginView.vue";
import StudentView from "./views/StudentView.vue";

const routes = [
    { path: "/", redirect: "/students" },
    { path: "/login", component: LoginView },
    { path: "/students", component: StudentView }
];

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.path !== '/login') {
        let token = localStorage.getItem('token');
        if (!token) {
            next('/login');
        } else {
            next();
        }
    } else {
        next();
    }
});

export default router;
